package com.fish.gaiety.gateway.configuration;

import com.fish.gaiety.gateway.base.BaseAudit;
import com.fish.gaiety.gateway.base.GenericResponseBody;
import com.fish.gaiety.gateway.base.ResponseApi;
import com.fish.gaiety.gateway.service.AuditService;
import com.fish.gaiety.gateway.util.BaseResponseCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Aspect
@Component
@Slf4j
@RequiredArgsConstructor
public class AuditLogAspect {

    private final AuditService auditService;

    @Around("@annotation(com.fish.gaiety.gateway.base.BaseAudit)")
    public Object execute(ProceedingJoinPoint pj){

        MethodSignature ms = (MethodSignature) pj.getSignature();
//        BaseAudit annotations =
//                AnnotationUtils.findAnnotation(ms.getMethod(), BaseAudit.class);
//        assert annotations!= null;

        auditService.audit("Start with " + ms.getMethod().getName());

        try {
            Object obj = pj.proceed();

            auditService.audit("End " + ms.getMethod().getName());

            return obj;
        } catch (Throwable ignore){
        }

        return null;
    }
}
